package org.framework.common.api.service.bo;

import java.util.List;

import org.framework.common.api.entity.CommonRegion;
import org.framework.common.api.service.dao.CommonRegionDao;
import org.framework.common.api.support.model.ChosenModel;
import org.framework.common.ext.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("regionBo")
public class CommonRegionBo extends CommonBoAdapter<CommonRegion, String> {
	
	@Autowired
	protected CommonRegionDao dao;

	/**
	 * 获取行政区划.
	 * @param model
	 * @return
	 * @throws Exception
	 * @author chanlong(陈龙)
	 * @date 2016年3月13日 上午9:44:55
	 */
	public Object getRegions(final CommonRegion model) throws Exception {
		String zone = model.getZone();
		String level = model.getLevel();
		
		List<CommonRegion> result = null;
		
		if (StringUtil.isNotEmpty(zone) && StringUtil.isNotEmpty(level)) {
			result = dao.findByPidAndZoneAndLevel(model.getPid(), zone, level);
		} else if (StringUtil.isNotEmpty(zone)) {
			result = dao.findByPidAndZone(model.getPid(), zone);
		} else if (StringUtil.isNotEmpty(level)) {
			result = dao.findByPidAndLevel(model.getPid(), level);
		} else {
			result = dao.findByPid(model.getPid());
		}
		
		return ChosenModel.create(result);
	}
}
